package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    //  插入数据
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{check_out_time} where id = #{id}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime check_out_time, Long id);

    //  分页查询
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    //  根据id查询
    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);

    //取消订单
    @Update("update orders set status = #{ordersStatus},pay_status = #{orderPaidStatus},cancel_time = #{cancelTime} where id = #{id}")
    void cancel(Long id,  Integer ordersStatus,  Integer orderPaidStatus, LocalDateTime cancelTime);

    // 统计订单数量
    Integer getOrderCountByStatus(Integer status);

    //  接单
    @Update("update orders set status = #{newStatus} where id = #{id}")
    void takeOrders(Long id, Integer newStatus);

    //  拒单
    @Update("update orders set status = #{cancelled},rejection_reason = #{rejectionReason},cancel_time = #{cancelTime} where id = #{id}")
    void rejectOrder(Long id, String rejectionReason, Integer cancelled, LocalDateTime cancelTime);

    //  取消订单
    @Update("update orders set status = #{cancelled},cancel_reason = #{cancelReason},cancel_time = #{cancelTime} where id = #{id}")
    void cancelOrders(Long id, String cancelReason, LocalDateTime cancelTime, Integer cancelled);

    //  派送订单
    @Update("update orders set status = #{deliveryInProgress} where id = #{id}")
    void deliveryOrders(Long id, Integer deliveryInProgress);

    //  完成订单
    @Update("update orders set status = #{completed},delivery_time = #{finishTime} where id = #{id}")
    void finishOrders(Long id, LocalDateTime finishTime, Integer completed);

    // 查询超时订单
    @Select("select * from orders where status = #{pendingPayment} and order_time < #{time}")
    List<Orders> getStatusWithTimeOut(Integer pendingPayment, LocalDateTime time);

    // 统计营业额
    Double getTurnoverByDate(Map map);

    // 统计订单总数
    Integer getOrderCount(Map map);

    //销量top10
    List<GoodsSalesDTO>  getSalesTop10(LocalDateTime  begin, LocalDateTime end);
}
